package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.FinancialDetailAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.WalletDetailInfo;
import com.haotunet.app.goldtreasure.model.WalletDetailModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

public class FinanicalDetailActivity extends BaseActivity {
    private TitleView mTitleView;
    private PullableListView lvFinanical;
    private PullToRefreshLayout mfreshLayout;
    private LinearLayout noFinancial;
    private int pageNum = 20;
    private final int GET_WALLET_DETAIL_SUCCESS = 2;
    private final int LOAD_MORE_WALLET_DETAIL_SUCCESS = 3;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GET_WALLET_DETAIL_SUCCESS:
                    WalletDetailModel model = (WalletDetailModel) msg.obj;
                    List<WalletDetailInfo> list = model.getDetailList();
                    if (list.size() == 0) {
                        noFinancial.setVisibility(View.VISIBLE);
                        return;
                    }
                    financialDetailAdapter = new FinancialDetailAdapter(FinanicalDetailActivity.this, list);
                    financialDetailAdapter.setDatas(list);
                    lvFinanical.setAdapter(financialDetailAdapter);
                    break;
                case LOAD_MORE_WALLET_DETAIL_SUCCESS:
                    WalletDetailModel moreModel = (WalletDetailModel) msg.obj;
                    if (moreModel.getDetailList().size() == 0) {
                        UIHelper.showToast(FinanicalDetailActivity.this, getString(R.string.last_page_hint));
                    }
                    financialDetailAdapter.addDatas(moreModel.getDetailList());
                    break;
                default:
                    break;
            }
        }

    };
    private FinancialDetailAdapter financialDetailAdapter;
    private ActionUtil actionUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finanical_detail);
        initView();
        initData("0", "");
    }

    private void initData(String pagenum, final String action) {
        actionUtil = new ActionUtil(FinanicalDetailActivity.this);
        actionUtil.getWalletDetail(pagenum + "");
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                WalletDetailModel walletModel = (WalletDetailModel) model;
                if (walletModel == null) {
                    UIHelper.showToast(FinanicalDetailActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (walletModel.getErrorMsg() != null) {
                    if (walletModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(FinanicalDetailActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(FinanicalDetailActivity.this, walletModel.getErrorMsg());
                    }
                } else {
                    if (action.equals("refresh") || action.equals("loadmore")) {
                        mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    Message msg = new Message();
                    if (action.equals("loadmore")) {
                        msg.what = LOAD_MORE_WALLET_DETAIL_SUCCESS;
                        pageNum = pageNum + 20;
                    } else {
                        msg.what = GET_WALLET_DETAIL_SUCCESS;
                    }
                    msg.obj = walletModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {
        mTitleView = (TitleView) findViewById(R.id.title_view);
        mTitleView.getTitletextView().setVisibility(View.VISIBLE);
        mTitleView.getTitletextView().setText("资金明细");
        mTitleView.getBackView().setVisibility(View.VISIBLE);
        mTitleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        noFinancial = (LinearLayout) findViewById(R.id.no_financial_layout);
        mfreshLayout = (PullToRefreshLayout) findViewById(R.id.id_swipe_ly);
        lvFinanical = (PullableListView) findViewById(R.id.lv_financial_detail);
        RefreshData(0);
    }

    private void RefreshData(final int pagenum) {
        mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                initData(pagenum + "", "refresh");
                pageNum = 20;
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                initData(pageNum + "", "loadmore");
            }
        });
    }
}
